package com.youlu.campus.common.base;

import com.youlu.campus.common.base.enums.CodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回值
 *
 * @param <T>
 */
@Data
public class QResult<T> implements Serializable {

    private T data;

    private int code;

    private String msg;

    public QResult() {
    }

    public QResult(CodeEnum codeEnums) {
        this.code = codeEnums.getCode();
        this.msg = codeEnums.getMessage();
    }

    public static <T> QResult<T> fail() {
        QResult q = new QResult(CodeEnum.FAIL);
        return q;
    }

    public static <T> QResult<T> fail(CodeEnum codeEnums) {
        QResult q = new QResult(codeEnums);
        return q;
    }

    public static <T> QResult<T> fail(String msg) {
        return fail(CodeEnum.FAIL.getCode(), msg);
    }

    public static <T> QResult<T> fail(int code, String msg) {
        QResult q = new QResult();
        q.setCode(code);
        q.setMsg(msg);
        return q;
    }

    public static <T> QResult<T> success(T data) {
        QResult<T> q = new QResult<T>(CodeEnum.SUCCESS);
        q.setData(data);
        return q;
    }

    public static <T> QResult<T> success() {
        QResult<T> q = new QResult<T>(CodeEnum.SUCCESS);
        return q;
    }
}
